package com.xauto.pioneer.appuser.activity.mine;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xauto.pioneer.appuser.MyApplication;
import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.activity.mine.balance.BalanceActivity;
import com.xauto.pioneer.appuser.activity.mine.user.GuideCategoryListActivity;
import com.xauto.pioneer.appuser.activity.mine.user.MemberMessageListActivity;
import com.xauto.pioneer.appuser.activity.mine.user.MineSettingActivity;
import com.xauto.pioneer.appuser.activity.user_aptitude.UsreAptitudeActivity;
import com.xauto.pioneer.appuser.http.API;
import com.xauto.pioneer.appuser.http.Constants;
import com.xauto.pioneer.appuser.interfaces.MemberCallBack;
import com.xauto.pioneer.appuser.model.Member;
import com.xauto.pioneer.appuser.util.DataCheckUtil;
import com.xauto.pioneer.appuser.util.DataLoadUtil;
import com.xauto.pioneer.appuser.util.StringUtils;
import com.xauto.pioneer.appuser.view.PromptDialog;
import com.xauto.pioneer.appuser.view.TopBar;

import butterknife.Bind;
import butterknife.OnClick;

/***
 * 个人中心页面
 * @author Lemon Sun 12 Feb 17:50
 */
public class MineActivity extends BaseActivity {

    @Bind(R.id.topBar) TopBar topBar;
    /** 用户头像*/
    @Bind(R.id.ivMineUserAvatar) ImageView ivMineUserAvatar;
    /** 用户等级*/
    @Bind(R.id.tvMineVip) TextView tvMineVip;
    /** 用户昵称*/
    @Bind(R.id.tvMineNickname) TextView tvMineNickname;
    /** 用车资质*/
    @Bind(R.id.tvMineAptitude) TextView tvMineAptitude;
    /** 手机号*/
    @Bind(R.id.tvMineMobile) TextView tvMineMobile;
    /** 我的余额*/
    @Bind(R.id.tvMineBalance) TextView tvMineBalance;
    /** 我的订单*/
    @Bind(R.id.tvMineOrder) TextView tvMineOrder;
    /** 我的卡卷*/
    @Bind(R.id.tvMineCoupon) TextView tvMineCoupon;
    private PromptDialog promptDialog;

    private Member member;
    private ImageLoader imageLoader;
    private DisplayImageOptions options;

    @Override
    protected int getContentView() {
        return R.layout.ac_mine;
    }

    @Override
    protected void initView() {
        topBar.showView(TopBar.TITLE, TopBar.LEFT_IMAGE_BTN, TopBar.RIGHT_IMAGE_BTN);
        topBar.setText(TopBar.TITLE, getString(R.string.top_bar_mine));
        topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        topBar.setImage(TopBar.RIGHT_IMAGE_BTN, R.mipmap.ic_top_bar_message_one);
        topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        topBar.setRightOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转消息页面
                MemberMessageListActivity.startAction(MineActivity.this);
            }
        });

        promptDialog = new PromptDialog(mContext, getString(R.string.notice), getString(R.string.text_not_network),
                getString(R.string.to_setting), getString(R.string.logout), getString(R.string.dialogpic_notice),new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //去设置
                promptDialog.dismiss();
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //退出
                promptDialog.dismiss();
            }
        });
    }

    @Override
    protected void initData() {
        imageLoader = ImageLoader.getInstance();
        options = MyApplication.getDisplayImageOptions(mContext, 40, R.mipmap.ic_default_user_avatar);
        member = DataCheckUtil.getMemberObj(mContext);
        setData();

    }

    @Override
    protected void onResume() {
        super.onResume();
        //刷新页面
        DataLoadUtil.refreshMemberData(mContext, new MemberCallBack() {
            @Override
            public void getMember(Member m) {
                member = m;
                setData();
            }
        });
    }
    void setData() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                imageLoader.displayImage(member.icon_image.thumb, ivMineUserAvatar, options);
                tvMineNickname.setText(!StringUtils.isBlank(member.member_name) ? member.member_name : "请输入姓名");
                tvMineMobile.setText(StringUtils.hideMobile(member.mobile));
                tvMineBalance.setText(member.member_account_info.balance_amount + "元");
                tvMineVip.setText(member.member_level_info.member_level_name);
                tvMineOrder.setText(StringUtils.isBlank(member.member_order_info.order_pending_payment_count) ? "" : "有" + member.member_order_info.order_pending_payment_count + "笔待付款订单");
                tvMineCoupon.setText(StringUtils.isBlank(member.member_voucher_info.voucher_useable_count) ? "" : member.member_voucher_info.voucher_useable_count + "张可用");
                if(member.member_message_info.is_new_message.equals("1")){
                    topBar.setImage(TopBar.RIGHT_IMAGE_BTN, R.mipmap.ic_top_bar_message_two);
                }else{
                    topBar.setImage(TopBar.RIGHT_IMAGE_BTN, R.mipmap.ic_top_bar_message_one);
                }
                //用车资质
               /* if(member.member_auth_info.member_auth_status.equals("1")) {
                    tvMineAptitude.setText(getString(R.string.user_set_card_verified));
                }
                if(member.member_auth_info.member_auth_status.equals("0")) {
                    tvMineAptitude.setText(getString(R.string.user_set_card_ver));
                }
                if(member.member_auth_info.member_auth_status.equals("2")) {
                    tvMineAptitude.setText(getString(R.string.user_set_card_verify));
                }*/
                //资质认证
                if(member.member_auth_info.member_auth_status.equals("1")&&member.member_deposit_info.is_deposit.equals("1")) {
                    tvMineAptitude.setText(getString(R.string.user_set_card_verified));
                }
                if(member.member_auth_info.member_auth_status.equals("0")&&member.member_deposit_info.is_deposit.equals("1")) {
                    tvMineAptitude.setText(getString(R.string.user_set_card_verify));
                }
                if(member.member_auth_info.member_auth_status.equals("0")&&member.member_deposit_info.is_deposit.equals("0")) {
                    tvMineAptitude.setText(getString(R.string.user_set_card_ver));
                }
                if(member.member_auth_info.member_auth_status.equals("0")&&member.member_deposit_info.is_deposit.equals("2")) {
                    tvMineAptitude.setText(getString(R.string.user_value_verify));
                }
                if(member.member_auth_info.member_auth_status.equals("1")&&member.member_deposit_info.is_deposit.equals("0")) {
                    tvMineAptitude.setText(getString(R.string.user_value_verify));
                }
                if(member.member_auth_info.member_auth_status.equals("1")&&member.member_deposit_info.is_deposit.equals("2")) {
                    tvMineAptitude.setText(getString(R.string.user_value_verify));
                }
                if(member.member_auth_info.member_auth_status.equals("2")&&member.member_deposit_info.is_deposit.equals("1")) {
                    tvMineAptitude.setText(getString(R.string.user_set_ing));
                }
                if(member.member_auth_info.member_auth_status.equals("2")&&member.member_deposit_info.is_deposit.equals("0")) {
                    tvMineAptitude.setText(getString(R.string.user_value_verify));
                }
                /*if(member.member_auth_info.member_auth_status.equals("2")&&member.member_auth_info.id_number_with_people_auth_status.equals("2")
                        &&member.member_auth_info.driving_license_auth_status.equals("2")&&member.member_auth_info.id_number_bank_status.equals("1")) {
                    tvMineAptitude.setText(getString(R.string.user_set_card_verify));
                }*/
            }
        });
    }

    @OnClick({R.id.llMineAptitude,R.id.ivMineUserAvatar,R.id.tvMineNickname,R.id.tvMineMobile,
            R.id.llMineBalance, R.id.llMineOrder, R.id.llMineCoupon, R.id.llMineCouponActive,
            R.id.llMineUserGuide, R.id.llMineSetting,R.id.ll_parkingorder,R.id.ll_parkingrent,
            R.id.ll_parkingautopay,R.id.ll_receipt})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivMineUserAvatar:
                //用户头像
                startActivityForResult(new Intent(mContext, MineSettingActivity.class), Constants.REQUEST_EDIT);
                break;
            case R.id.tvMineNickname:
                //用户头像
                startActivityForResult(new Intent(mContext, MineSettingActivity.class), Constants.REQUEST_EDIT);
                break;
            case R.id.tvMineMobile:
                //用户头像
                startActivityForResult(new Intent(mContext, MineSettingActivity.class), Constants.REQUEST_EDIT);
                break;
            case R.id.llMineAptitude:
                //用车资质
                startActivity(new Intent(mContext, UsreAptitudeActivity.class));
                break;
            case R.id.llMineBalance:
                //我的余额
                startActivity(new Intent(mContext, BalanceActivity.class));
                break;
            case R.id.llMineOrder:
                //用车订单
                startActivity(new Intent(mContext, OrderActivity.class));
                break;
            case R.id.llMineCoupon:
                //用车卡卷
//                startActivity(new Intent(mContext, CouponActivity.class));
                VoucherListActivity.startAction(MineActivity.this,false,"",101, API.GET_VOUCHER_LIST);
                break;
            case R.id.ll_parkingorder:
                //停车订单
                startActivity(new Intent(mContext, ParkingOrderActivity.class));
//                VoucherListActivity.startAction(MineActivity.this,false,"",101, API.GET_VOUCHER_LIST);
                break;
            case R.id.ll_parkingrent:
                //车位长租
                startActivity(new Intent(mContext, ParkingRentListActivity.class));
//                VoucherListActivity.startAction(MineActivity.this,false,"",101, API.GET_VOUCHER_LIST);
                break;
            case R.id.ll_parkingautopay:
                //停车自缴
                startActivity(new Intent(mContext, ParkingAutoPayActivity.class));
//                VoucherListActivity.startAction(MineActivity.this,false,"",101, API.GET_VOUCHER_LIST);
                break;
            case R.id.ll_receipt:
                //开取发票
                startActivity(new Intent(mContext, ReceiptActivity.class));
//                VoucherListActivity.startAction(MineActivity.this,false,"",101, API.GET_VOUCHER_LIST);
                break;
            case R.id.llMineCouponActive:
                //优惠活动
                //TODO
//                WebViewActivity.startAction(mContext,6);
                break;
            case R.id.llMineUserGuide:
                //使用指南
                startActivity(new Intent(mContext, GuideCategoryListActivity.class));
                break;
            case R.id.llMineSetting:
                //设置中心
                startActivityForResult(new Intent(mContext, SettingActivity.class), Constants.REQUEST_CLOSE_PAGE);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == Constants.REQUEST_CLOSE_PAGE && resultCode == Constants.RESULT_CLOSE_PAGE) {
            //退出登录，关闭当前页面
            finish();
        }
        if(requestCode == Constants.REQUEST_EDIT && resultCode == Constants.RESULT_EDIT_SUCCESS) {
            //编辑会员成功，刷新页面
            DataLoadUtil.refreshMemberData(mContext, new MemberCallBack() {
                @Override
                public void getMember(Member member) {
                    setData();
                }
            });
        }
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}
